/**
 * 数据存储操作
 */
class StorageFn {
    static isNull(input) {
        return input === undefined || input === null;
    }

    /*-----------------cookie---------------------*/

    /**
     * @desc  设置Cookie
     * @param {String} name
     * @param {String} value
     * @param {Number} days
     */
    static setCookie(name, value, days) {
        let date = new Date();
        date.setDate(date.getDate() + days);
        document.cookie = name + '=' + value + ';expires=' + date;
    }

    /**
     * @desc 根据name读取cookie
     * @param  {String} name
     * @return {String}
     */
    static getCookie(name) {
        const arr = document.cookie.replace(/\s/g, "").split(';');
        for (let i = 0; i < arr.length; i++) {
            let tempArr = arr[i].split('=');
            if (tempArr[0] === name) {
                return decodeURIComponent(tempArr[1]);
            }
        }
        return '';
    }

    /**
     * @desc 根据name删除cookie
     * @param  {String} name
     */
    static removeCookie(name) {
        // 设置已过期，系统会立刻删除cookie
        this.setCookie(name, '1', -1);
    }


    /*-----------------localStorage---------------------*/

    /**
     * @desc  设置localStorage
     * @param {*} name
     * @param {String} value
     */
    static saveLocal(name, value) {
        if (window.localStorage && JSON && name) {
            if (typeof value === 'object') {
                value = JSON.stringify(value);
            }
            window.localStorage[name] = value;
            return true;
        }
        return false;
    }

    /**
     * @desc 根据name和类型读取localStorage
     * @param  {String} name
     * @param  {String} type ''|'json'
     * @return {String}
     */
    static getLocal(name, type) {
        if (window.localStorage && JSON && name) {
            const data = window.localStorage[name];
            if (type && type === 'json' && data !== undefined) {
                try {
                    return JSON.parse(data);
                } catch (e) {
                    console.error(`取数转换json错误${e}`);
                    return '';
                }
            } else {
                return data;
            }
        }
        return null;
    }

    /**
     * @desc 根据name读取localStorage
     * @param  {String} name
     * @return {String}
     */
    static getLocal2Json(name) {
        if (window.localStorage && JSON && name) {
            const data = window.localStorage[name];
            if (!this.isNull(data)) {
                try {
                    return JSON.parse(data);
                } catch (e) {
                    console.error(`取数转换json错误${e}`);
                    return '';
                }
            } else {
                return data;
            }
        }
        return null;
    }

    /**
     * @desc 根据name移除localStorag
     * @param  {String} name
     */
    static removeLocal(name) {
        if (window.localStorage && name) {
            window.localStorage[name] = null;
        }
        return null;
    }

    /**
     * @desc 移除所有localStorag
     */
    static clearLocal() {
        if (window.localStorage) {
            window.localStorage.clear()
        }
    }


    /*-----------------sessionStorage---------------------*/


    /**
     * @desc  设置sessionStorage
     * @param {*} name
     * @param {String} value
     */
    static setSession(name, value) {
        if (window.sessionStorage && JSON && name) {
            if (typeof value === 'object') {
                value = JSON.stringify(value);
            }
            window.sessionStorage.setItem(name, JSON.stringify(value));
            return true;
        }
        return false;
    }

    /**
     * @desc 根据name和类型读取sessionStorage
     * @param  {String} name
     * @param  {String} type ''|'json'
     * @return {String}
     */
    static getSession(name, type) {
        if (window.sessionStorage && JSON && name) {
            const data = window.sessionStorage.getItem(name);
            if (type && type === 'json' && data !== undefined) {
                try {
                    return JSON.parse(data);
                } catch (e) {
                    console.error(`取数转换json错误${e}`);
                    return '';
                }
            } else {
                return data;
            }
        }
        return null;
    }

    /**
     * @desc 根据name读取sessionStorage
     * @param  {String} name
     * @return {String}
     */
    static getSession2Json(name) {
        if (window.sessionStorage && JSON && name) {
            const data = window.sessionStorage.getItem(name);
            if (!this.isNull(data)) {
                try {
                    return JSON.parse(data);
                } catch (e) {
                    console.error(`取数转换json错误${e}`);
                    return '';
                }
            } else {
                return data;
            }
        }
        return null;
    }


    /**
     * @desc 根据name移除sessionStorage
     * @param  {String} name
     */
    static removeSession(name) {
        if (window.sessionStorage && name) {
            window.sessionStorage.removeItem(name)
        }
        return null;
    }

    /**
     * @desc 移除所有sessionStorage
     */
    static clearSession() {
        if (window.sessionStorage) {
            window.sessionStorage.clear()
        }
    }

}

module.exports = StorageFn;